package com.handpay.nocardpay.baselibrary.lottery;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.widget.CustomWebView;

import java.security.PublicKey;


public class LotteryFragment extends BaseFragment {
    private WebView mWebView;
    public static LotteryFragment newInstance() {
        Bundle args = new Bundle();
        LotteryFragment fragment = new LotteryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_lottery;
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getResources().getString(R.string.lottery);
    }

    @Override
    public void initView(View view) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(toolbar, true);
        mWebView = (WebView) view.findViewById(R.id.wv_content);
    }

    @Override
    public void initListener() {
        mWebView.getSettings().setDomStorageEnabled(true);
        // 设置支持javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        String host;

        if (NoCardPayConfig.isTesting) {
            host = ClientEngine.getInstance().LOTTERY_URL_TEST;
        } else {
            host = "https://scan.handpay.cn/hpayMicroWap/prize/showClientPrizeIndex.htm?";
        }

        String userId = SecureManager.getInstance().des(AccountUtils.getUserId(_mActivity), 1, NoCardPayConfig.KEY_SECURE);
        String merchantCode = SecureManager.getInstance().des(AccountUtils.getMerchantCode(_mActivity), 1, NoCardPayConfig.KEY_SECURE);
        String channel = SecureManager.getInstance().des(NoCardPayConfig.CHANNEL, 1, NoCardPayConfig.KEY_SECURE);
        String url = host +  "loginUserId=" + userId + "&merchantCode=" + merchantCode + "&channel=" + channel;
        HPLog.d(HPLog.SSX_TAG, url);
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                HPLog.d(HPLog.SSX_TAG, "onPageStarted");
                DialogUtils.showProgressDialog(_mActivity, null, _mActivity.getResources().getString(R.string.loading), false);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                HPLog.d(HPLog.SSX_TAG, "onPageFinished");
                DialogUtils.closeProgressDialog();
                super.onPageFinished(view, url);
            }
        });
    }
}
